<?php

class subscriber_model extends CI_Model 
{
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

	// Get Subscriber based on Subscriber ID
	function getSubscriberByID( $subscriber_id )
	{
		$this->db->where('subscriber_id', $subscriber_id);
		$this->db->from('tbl_subscriber');
		return $this->db->get();
	}
	
	function getSubscribersByLastName( $last_name )
	{
		$this->db->like('last_name', $last_name);
		$this->db->from('tbl_subscriber');
		$this->db->limit(10);
		return $this->db->get();
	}
	
	// Get last Subscriber ID
	function getLastSubscriberID( )
	{
		$this->db->select_max('subscriber_id');
		return $this->db->get('tbl_subscriber')->row()->subscriber_id;
	}

	// Delete subscriber based on Subscriber ID
	function deleteSubscriberByID( $subscriber_id )
	{
		$delete_where_array = array('subscriber_id' => $subscriber_id );
	
		$this->db->delete('tbl_subscriber', $delete_where_array );
	}
	
	// Add Subscriber
	function addSubscriber( $data_array )
	{
		$this->db->insert('tbl_subscriber', $data_array); 
	}
	
	// Updates subscribers based on Subscriber ID
	function updateSubscriber( $subscriber_id, $data_array )
	{
		$update_where_array = array('subscriber_id' => $subscriber_id );
	
		$this->db->update('tbl_subscriber', $data_array, $update_where_array); 
	}

	// Search Subscriber Table
	function getSearchResults( $search_by='last_name', $terms='ALL', $offset=0, $limit=20 )
	{
		// If we do not want all results
		if ($terms != 'ALL')
		{
			$term_array = explode('%20', $terms);
			foreach ($term_array as $word)
				$this->db->like($search_by, $word);
		}
		$this->db->from('tbl_subscriber');
		$this->db->order_by($search_by, 'asc');
		$this->db->limit($limit, $offset);
		return $this->db->get();
	}
	
	// Get Total Number of Results for Search
	function getSearchCount( $search_by='last_name', $terms='ALL' )
	{
		if ($terms != 'ALL')
		{
			$term_array = explode('%20', $terms);
			foreach ($term_array as $key=>$word)
				$this->db->like($search_by, $word);
		}
		$this->db->from('tbl_subscriber');
		return $this->db->count_all_results();
	}
}

?>